import {Component, OnInit} from '@angular/core';

``;
import {HttpClient} from '@angular/common/http';
import {AppUrl} from '../../../app.url';
import {TinymceConfig} from '../../../common/tinymce.config';
import {NzMessageService} from 'ng-zorro-antd';

@Component({
    selector: 'app-spon-publish',
    templateUrl: './spon-publish.component.html',
    styleUrls: ['./spon-publish.less']
})
export class SponPublishComponent implements OnInit {
    ckeditorContent = '';
    config = TinymceConfig;
    private dictData: any = {};
    private supMaters = [];
    private supPropaWays = [];
    private supFeedbacks = [];
    selectedOption;
    provs = [
        {value: '1', label: '云南省'},
        {value: '2', label: '贵州省'},
    ];
    cities = [
        {value: '1', label: '丽江市'},
        {value: '2', label: '大理市'},
    ];

    constructor(private http: HttpClient,
                private nzMessage: NzMessageService) {
    }


    hotList = [
        {
            text: '怎么获取全网套餐增值服务',
        },
        {
            text: '全网套餐增值服务',
        },
        {
            text: '可以选择不同意吗？',
        }
    ];

    loading = false;

    provList = [];

    _options;

    _value;

    _disabledDate(current: Date): boolean {
        return current && current.getTime() < Date.now();
    }

    changeSelect(option) {
        this.nzMessage.info(option.province + ' ' + option.id);
    }

    searchChange() {
        if (this.selectedOption === '') {

        }
    }

    getProfessionCategoryOptions(SubjectCategory: string) {

        console.log('text');
        console.log(SubjectCategory);
    }

    showVal() {
        console.log(this._value);
    }

    private dict = [
        'sup_type',
        'percent',
        'commodity',
        'population',
        'propaganda',
        'feedback'
    ];
    ngOnInit() {
        this.http.post(AppUrl.province.selectAllUrl, {}).subscribe(data => {
            console.log(data);
            let temp: any = data;
            this.provList = temp.data;
        });
        this.http.post(AppUrl.province.selectWithCity, {}).subscribe(data => {
            let temp: any = data;
            this._options = temp.data;
        });
        this.http.post(AppUrl.dict.getKeyUrl, this.dict).subscribe(data => {
            let temp: any = data;
            console.log(temp.data);
            for (let item of this.dict) {
                this.dictData[item] = temp.data.filter(e => e['type'] === item);
            }

            this.supMaters = this.dictData['commodity'].map(_ => {
                return {label: _.key, value: _.key, checked: false};
            });

            this.supPropaWays = this.dictData['propaganda'].map(_ => {
                return {label: _.key, value: _.key, checked: false};
            });

            this.supFeedbacks=this.dictData['feedback'].map(_ => {
                return {label: _.key, value: _.key, checked: false};
            });
        });
    }

    private support = {
        topic: '',
        type: '',
        amount: 0,
        percent: '',
        commodity: '',
        sDate: '',
        eDate: '',
        city: '',
        population: '',
        propaganda: '',
        feedback: '',
        supDetail: '',
        status: '0',
    };
    private begin: any = '';
    private end: any = '';

    validateValue(): boolean {
        let result: boolean = false;
        if (this.support.topic.length <= 22 && this.support.topic.length > 0) {
            if (this.support.type !== '') {
                if (this.support.amount >= 300) {
                    if (this.begin !== '' && this.end !== '') {
                        if (this.begin <= this.end) {
                            if (this.support.percent !== '') {
                                if (this.ckeditorContent !== '') {
                                    if (this._value.length > 0) {
                                        if (this.support.commodity !== '') {
                                            if (this.support.propaganda !== '') {
                                                if (this.support.feedback !== '') {
                                                    if (this.support.population !== '') {
                                                        result = true;
                                                    }
                                                    else {
                                                        this.nzMessage.error('请输入参与人数');
                                                    }
                                                }
                                                else this.nzMessage.error('请选择赞助反馈方式');
                                            }
                                            else this.nzMessage.error('请选择宣传方式');
                                        }
                                        else this.nzMessage.error('请选择赞助物资');
                                    }
                                    else this.nzMessage.error('请选择省份和城市');
                                }
                                else this.nzMessage.error('赞助介绍不能为空');
                            }
                            else {
                                this.nzMessage.error('赞助预付比例不能为空');
                            }
                        }
                        else {
                            this.nzMessage.error('赞助开始时间不能小于结束时间');
                        }
                    }
                    else {
                        this.nzMessage.error('请选择赞助开始时间和结束时间');
                    }
                }
                else {
                    this.nzMessage.error('预算金额不能小于300');
                }
            }
            else {
                this.nzMessage.error('赞助类型不能为空');
            }
        }
        else {
            this.nzMessage.error('赞助名称必填且不超过22位');
        }
        return result;
    }

    //发布赞助按扭事件
    publishSupport() {
        this.loading = true;
        this.support.city = '';
        for (let i = 0; i < this._value.length; i++) {
            if (i < this._value.length - 1)
                this.support.city += this._value[i] + ',';
            else this.support.city += this._value[i];
        }
        this.support.commodity = this.checkOptionsConnect(this.supMaters);
        this.support.propaganda = this.checkOptionsConnect(this.supPropaWays);
        this.support.feedback = this.checkOptionsConnect(this.supFeedbacks);

        this.support.supDetail = this.ckeditorContent;
        if (this.validateValue()) {

            const id = this.nzMessage.loading('发布中', {nzDuration: 30000}).messageId;

            if (typeof(this.begin) !== 'string') {
                this.support.sDate = this.dateToString(this.begin);
                this.support.eDate = this.dateToString(this.end);
            }

            this.http.get(AppUrl.common.getUserIdUrl).subscribe(data => {
                let temp: any = data;
                if (temp.code == 200) {
                    this.http.post(AppUrl.support.publishUrl, this.support)
                        .subscribe(data => {
                            let temp: any = data;
                            if (temp.code == 200) {
                                this.nzMessage.remove(id);
                                this.nzMessage.create('success', '发布成功', {nzDuration: 4000});
                                this.loading = false;
                            }
                            else if (temp.code == 2001) {
                                this.nzMessage.remove(id);
                                this.nzMessage.create('warning', '请先登录', {nzDuration: 4000});
                                this.loading = false;
                            }
                            else {
                                this.nzMessage.remove(id);
                                this.nzMessage.create('error', '系统繁忙，请稍后再试', {nzDuration: 4000});
                                this.loading = false;
                            }

                        }, onError => {
                            this.nzMessage.error('出错了，请稍后再试');
                            this.loading = false;
                        });
                }
                else
                    this.nzMessage.create('error', '登陆信息超时，请重新登录', {nzDuration: 4000});
            });
        }
        else {
            this.loading = false;
        }
    }

    dateToString(date): string {
        return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
    }

    //逗号拼接多选结果
    checkOptionsConnect(o: any): string {
        let temp = '';
        o.filter(data => data.checked === true).forEach(data => {
                if (temp === '') {
                    temp = data.value;
                }
                else
                    temp = temp + ',' + data.value;
            }
        );
        return temp;
    }
}
